/**
 * VYPe11 Project
 * VYPe11 language compiler.
 * Authors: Jozef Lang (xlangj01), 
 * 			Zoltan Zemko (xzemko01)
 * 2011
 */
package cz.vutbr.fit.VYPe.team18.VYPe11.nodes.expression.terminal;

import java.io.IOException;

import cz.vutbr.fit.VYPe.team18.VYPe11.exception.SematicException;
import cz.vutbr.fit.VYPe.team18.VYPe11.nodes.IdentifierNode;
import cz.vutbr.fit.VYPe.team18.VYPe11.nodes.data_type.DataType;
import cz.vutbr.fit.VYPe.team18.VYPe11.nodes.expression.ExpressionListNode;
import cz.vutbr.fit.VYPe.team18.VYPe11.sematic.declared.Function;

public class FunctionInvocationNode extends TerminalExpression {
	private IdentifierNode id;
	private ExpressionListNode expressionList;
	
	public FunctionInvocationNode(IdentifierNode id,
								ExpressionListNode exprList) {
		this.id=id;
		this.expressionList=exprList;
	}

	public IdentifierNode getId() {
		return id;
	}

	public ExpressionListNode getExpressionList() {
		return expressionList;
	}
	
	public DataType getType() throws SematicException {
		if(expressionType!=null)
			return expressionType;
		Function fun=this.getDeclarationLevels().findFunctionByFunctionInvocationNode(this);
		expressionType=fun.getReturnType();
		return expressionType;
	}

	@Override
	public void generateTAC() throws IOException {
		expressionList.generateTAC();
		if(expressionType==DataType.VOID)
			this.writeTAC("call", this.id.getId(), "", "");
		else
			this.writeTAC("call", this.id.getId(), "", "_t"+tacCounter.getNextTermIndex());
	}
}
